package jsyf.oa.web.wreport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.entity.wreport.WReportCustomerEntity;
import jsyf.oa.entity.wreport.WReportEntity;
import jsyf.oa.facade.wreport.WReportFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/22 0022.
 */
@Api(value = "工作报告接口", description = "工作报告接口")
@Controller
@RequestMapping("/wreport")
public class WReportController {
    @Resource
    private WReportFacade wReportFacade;
    @ResponseBody
    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "写报告",
            notes = " 写报告",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{ \n" +
            "main: \n" +
            "{ id: '',\n" +
            " title: '卫泽洪2017-01-01工作报告', \n" +
            " userId:'2017112017054BC09E1982AFA67B34B4', \n" +
            "reportDate: '2017-01-01' }, \n" +
            "details: [ { id: '', content: '心得体会', type: '1', wId: '', userId: '2017112017054BC09E1982AFA67B34B4' } ,\n" +
            "{ id: '', content: '人员情况', type: '2', wId: '', userId: '2017112017054BC09E1982AFA67B34B4' },\n" +
            "{ id: '', content: '第二天计划', type: '3', wId: '', userId: '2017112017054BC09E1982AFA67B34B4' }  ] }", dataType = "String", paramType = "body")
    public  void save(HttpServletRequest request, HttpServletResponse response,  @RequestBody String jsonData){
        String result = wReportFacade.save(jsonData);
        if(result == ""){
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("没有配置上级,发送失败"));
        }else {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
        }
    }
    /**
     * @description: 查询工作报告
     * @creat: 15:40  2017/11/22 0022
     * @since: V1.0.0
     * @see jsyf.oa.web
     */
    @ResponseBody
    @RequestMapping(value = "/getByUserIdAndReportDate.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "查询工作报告",
            notes = " 查询工作报告",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{}", dataType = "String", paramType = "body")
    public  void getByUserIdAndReportDate(HttpServletRequest request, HttpServletResponse response,  @RequestBody String jsonData){
        JSONObject obj_params = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId= obj_params.getString("userId");
        String reportDate=obj_params.getString("reportDate");
        JSONObject object= wReportFacade.getByUserIdAndReportDate(userId,reportDate);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(object));
    }

    @ResponseBody
    @RequestMapping(value = "/getById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "查询工作报告",
            notes = " 查询工作报告",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{}", dataType = "String", paramType = "body")
    public  void getById(HttpServletRequest request, HttpServletResponse response,  @RequestBody String jsonData){
        JSONObject obj_params = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        WReportEntity wReportEntity  = wReportFacade.getById(obj_params.getString("id"));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond((JSONObject) JSON.toJSON(wReportEntity)));
    }
    @RequestMapping(value = "/getCustomerByTel.json", method = RequestMethod.POST)
    public void getCustomerByTel(HttpServletResponse response, @RequestBody String jsonData) {

        WReportCustomerEntity model=  this.wReportFacade.getCustomerByTel(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond((JSONObject) JSON.toJSON(model)));
    }

    @ResponseBody
    @RequestMapping(value = "/getCustomerType.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "查询客户类型名称",
            notes = " 查询客户类型名称",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{}", dataType = "String", paramType = "body")
    public  void getCustomerType(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData)
    {
        List<JSONObject> resultList = this.wReportFacade.getCustomerType();
        String result = "";
        if (resultList != null && resultList.size() > 0) {
            result = BaseUtils.succsessArrRespond(resultList);
        } else {
            result = BaseUtils.errorrRespond("暂无数据");
        }
        ResponseUtils.putJsonResponse(response, result);
    }

    @ResponseBody
    @RequestMapping(value = "/getExpenseByParentId.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "查询费用名称",
            notes = " 查询费用名称",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{}", dataType = "String", paramType = "body")
    public  void getExpenseByParentId(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData)
    {
        JSONObject obj_params = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String parentId = obj_params.getString("parentId");
        String userId = obj_params.getString("userId");

        List<JSONObject> resultList = this.wReportFacade.getExpenseByParentId(parentId,userId);
        String result = "";
        if (resultList != null && resultList.size() > 0) {
            result = BaseUtils.succsessArrRespond(resultList);
        } else {
            result = BaseUtils.errorrRespond("暂无数据");
        }
        ResponseUtils.putJsonResponse(response, result);
    }

    @RequestMapping(value = "/getReceiverAndCopiersByUserIdAndReportDate.json", method = RequestMethod.POST)
    public void getReceiverAndCopiersByUserIdAndReportDate(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData){
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        JSONObject result = wReportFacade.getReceiverAndCopiersByUserIdAndReportDate(jsonParam.getString("userId"),jsonParam.getString("reportDate"));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
    }

    @RequestMapping(value = "/getReceiverAndCopiers.json", method = RequestMethod.POST)
    public void getReceiverAndCopiers(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData){
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        JSONObject result = wReportFacade.getReceiverAndCopiers(jsonParam.getString("userId"));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
    }

    @RequestMapping(value = "/getList.json", method = RequestMethod.POST)
    public void getList(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData){
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        List<JSONObject> result = wReportFacade.getList(jsonParam.getInteger("year"),jsonParam.getInteger("month"),jsonParam.getString("userId"));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }
    @ResponseBody
    @RequestMapping(value = "/getDayCharge.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "当日费用明细",
            notes = " 当日费用明细",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{'userId':'','reportDate':''}", dataType = "String", paramType = "body")
    public  void getDayCharge(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData)
    {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        List<JSONObject> resultList=null;
        if(!"".equals(jsonParams.getString("userId"))&&!"".equals(jsonParams.getString("reportDate"))){
            resultList = this.wReportFacade.getDayCharge(jsonParams.getString("userId"),jsonParams.getString("reportDate"));
        }
        String result = BaseUtils.succsessArrRespond(resultList);
        ResponseUtils.putJsonResponse(response, result);
    }
}
